package com.hsq.java.lambda;

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.*;
import java.util.stream.Stream;

/**
 * @author hsq
 * @date 2025-04-06 下午3:23
 */
public class Lambda01 {

    public static void main(String[] args) {
//        foreachArr((int value) -> {
//            System.out.println(value);
//        });


    }


    @Test
    void test1() {
        List<Integer> list = new ArrayList<>();
        //Stream<Integer> stream = list.stream();
        //Integer[] arr = {1, 2};
        //Stream<Integer> stream = Arrays.stream(arr);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(4);
        list.add(5);
        list.stream()
                .distinct()
                .filter(integer -> integer > 2)
                .forEach(integer -> System.out.println(integer));
        Optional<Integer> min = list.stream()
                .distinct()
                .min(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                });
        System.out.println(min.get());
    }

    @Test
    void test2() {
        Map<String, Integer> map = new HashMap<>();
        map.put("aaa", 1);
        map.put("bbb", 2);
        map.put("ccc", 3);
        Stream<Map.Entry<String, Integer>> stream = map.entrySet().stream();
        stream.distinct()
                .filter((Map.Entry<String, Integer> entry) -> {
                        return entry.getValue() > 1;})
                .forEach(entry -> {
                        System.out.println(entry.getKey() + "====" + entry.getValue());});
    }

    @Test
    void test() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(4);
        list.add(5);
        Optional<List<Integer>> optionalList = Optional.ofNullable(list);
//        Optional<Integer> i1 = optionalList.flatMap(new Function<List<Integer>, Optional<Integer>>() {
//            @Override
//            public Optional<Integer> apply(List<Integer> integers) {
//                return Optional.ofNullable(integers.get(0));
//            }
//        });
        boolean anyMatch = list.stream()
                .anyMatch(val -> val >= 5);
        System.out.println(anyMatch);
        boolean b = list.stream()
                .allMatch(val -> val > 1);
        System.out.println(b);
        boolean noneMatch = list.stream()
                .noneMatch(val -> val > 5);
        System.out.println(noneMatch);
        Optional<Integer> any = list.stream()
                .filter(val -> val > 3)
                .findAny();//只会找到一个
        any.ifPresent(i -> System.out.println(i));
        list.stream()
                .map(integer -> {
                    integer = integer + 10;
                    return integer;
                })
                .forEach(System.out::println);
        System.out.println(list.get(0));

    }

    public static void foreachArr(IntConsumer intConsumer) {
        int[] arr = {1, 2, 3, 4, 5};
        for (int i: arr) {
            intConsumer.accept(i);
        }
    }
}
